﻿namespace KuiHuaBaoDian.Services.Net {

    /// <summary>
    /// 网络数据包ID
    /// </summary>
    public interface INetPacketID {

        /// <summary>
        /// 值
        /// </summary>
        object Value { get; }
    }

    /// <summary>
    /// 网络数据包ID
    /// </summary>
    /// <typeparam name="T">值类型</typeparam>
    public readonly struct NetPacketID<T> : INetPacketID {

        /// <summary>
        /// 值
        /// </summary>
        public T Value { get; init; }

        object INetPacketID.Value => Value;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="value">值</param>
        public NetPacketID(T value) {
            Value = value;
        }

        public override readonly int GetHashCode() => Value.GetHashCode();

        public override readonly bool Equals(object obj) {
            if (obj is NetPacketID<T> other) {
                return other.Value.Equals(Value);
            }
            return false;
        }

        public override readonly string ToString() {
            return $"[{GetType().Name}({Value})]";
        }

        public static bool operator ==(NetPacketID<T> left, NetPacketID<T> right) => left.Equals(right);
        public static bool operator !=(NetPacketID<T> left, NetPacketID<T> right) => !left.Equals(right);
    }

    public static class NetPacketIDExtensions {

        public static NetPacketID<uint> ToUInt(this INetPacketID @in) => new() { Value = (uint)@in.Value };

        public static NetPacketID<ushort> ToUShort(this INetPacketID @in) => new() { Value = (ushort)@in.Value };
    }
}
